package com.chiangleoi.appointment.appointments;


import com.alibaba.fastjson.JSON;
import com.chiangleoi.appointment.constant.DateFormatPattern;
import com.chiangleoi.appointment.model.AppointmentsJson;
import com.chiangleoi.appointment.model.PlaceJson;
import com.chiangleoi.appointment.model.UserJson;
import com.chiangleoi.appointment.model.fileJson.PlacesList;
import com.chiangleoi.appointment.model.fileJson.SitePositionList;
import com.chiangleoi.appointment.model.fileJson.VenueList;
import com.chiangleoi.appointment.utils.BeanCopyUtil;
import com.chiangleoi.appointment.utils.DateUtils;
import com.chiangleoi.appointment.utils.HttpUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ImageDownloadName {

    private final static Integer times = 10;


    public static void main(String[] args) throws Exception {

        String href = "https://dmp-ali.hikvision.com/NVR4.0lite%E7%89%88%E6%9C%AC%E6%9C%AC%E5%9C%B0%E8%A7%86%E5%9B%BE%E9%85%8D%E7%BD%AE%E2%80%94%E8%A7%86%E9%A2%91%E5%B0%81%E9%9D%A2_1722843188802.jpg_1722843191518.jpg";
        String fileName = getFileName(href);
        System.out.println(fileName);

    }

    /**
     * 根据Url获取文件名
     *
     * @param href
     * @return
     */
    public static String getFileName(String href) throws Exception {
        URL url = null;
        try {
            url = new URL(href);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.connect();//获取文件名和扩展名
        conn.getResponseCode();
        href = conn.getURL().toString();
        //获取header 确定文件名和扩展名，并防止乱码
        String filename = "";
        if (conn.getHeaderField("Content-Disposition") != null) {
            filename = new String(conn.getHeaderField("Content-Disposition").getBytes("ISO-8859-1"), "UTF-8");
        }
        filename = filename.indexOf("filename=") > -1 ? filename.substring(filename.lastIndexOf("filename=") + "filename=".length()) : filename;
        return filename;
    }

}
